package com.dragon.smtp.controller;


import com.dragon.smtp.domain.Mail;
import com.dragon.smtp.domain.User;
import com.dragon.smtp.service.IMailService;
import com.dragon.smtp.service.IMailboxService;
import com.dragon.smtp.service.impl.MailboxServiceImpl;
import com.dragon.smtp.util.DragonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小龙
 * @since 2022-11-08
 */
@RestController
@RequestMapping("/mail")
public class MailController {

    @Autowired
    IMailboxService mailboxService;
    @Autowired
    IMailService mailService;
    @PostMapping("/sendMail")
    DragonResult sendMail(@RequestBody Mail mail){
        System.out.println("from     "+mail.getFromEmail());
        System.out.println("to     "+mail.getToEmail());
        if(mailboxService.selectFrombox(mail.getFromEmail(),mail.getToEmail())==null){
            return DragonResult.fail("SMTP未开启");
        }
        return mailService.sendMail(mail);
    }
    @PostMapping("/sendDraftMail")
    DragonResult sendDraftMail(@RequestBody Mail mail){
        return mailService.sendDraftMail(mail);
    }
    @PostMapping("/getSentMail")
    DragonResult getSentMail(@RequestBody User user){

        return mailService.getSentMail(user);
    }
    @PostMapping("/getDraftMail")
    DragonResult getDraftMail(@RequestBody User user){

        return mailService.getSentDraftMail(user);
    }

    @PostMapping("/getDeletedMail")
    DragonResult getDeletedMail(@RequestBody User user){
        return mailService.getDeletedMail(user);
    }

    /**
     * 收件箱
     * @param user
     * @return
     */
    @PostMapping("/getMail")
    DragonResult getMail(@RequestBody User user){
        System.out.println("from     "+user);
        if(mailboxService.selectToBox(user.getUsername())==null){
            return DragonResult.fail("Pop3未开启");
        }
        return mailService.getMail(user);
    }

    @PostMapping("/getStarMail")
    DragonResult getStarMail(@RequestBody User user){
        System.out.println("from     "+user);
        if(mailboxService.selectToBox(user.getUsername())==null){
            return DragonResult.fail("Pop3未开启");
        }
        return mailService.getStarMail(user);
    }
    @GetMapping("/starMail")
    DragonResult starMail(String id){
        return mailService.starMail(id);
    }
    @GetMapping("/unstarMail")
    DragonResult unstarMail(String id){
        return mailService.unstarMail(id);
    }

    @GetMapping("/deleteMail")
    DragonResult deleteMail(String id){
        return mailService.deleteMail(id);
    }
    @GetMapping("/markDeletedMail")
    DragonResult markdeletedMail(String id){
        return mailService.markDeletedMail(id);
    }
    @GetMapping("/restoreDeletedMail")
    DragonResult restoreDeletedMail(String id){
        return mailService.restoreDeletedMail(id);
    }

}

